package cn.xiaoyangh.org.controller;

import cn.xiaoyangh.basic.util.JsonResult;
import cn.xiaoyangh.basic.util.PageList;
import cn.xiaoyangh.org.domain.Employee;
import cn.xiaoyangh.org.query.EmployeeQuery;
import cn.xiaoyangh.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(tags = "部门接口",description = "专门解刨宠物的研究中心")
public class EmployeeController {
    @Autowired
    private IEmployeeService es;

    @PutMapping
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    public JsonResult addOrUpdate(@RequestBody Employee d){

        try {
            if(d.getId()==null){
                es.save(d);
            }else {
                es.update(d);
            }
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "部门删除",notes = "删除档案所用")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            es.remove(id);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }
    @PatchMapping
    @ApiOperation(value = "部门批量删除",notes = "删除档案所用")
    public JsonResult deleteAll(@RequestBody List<Long> list){
        try {
            es.deleteAll(list);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询单个",notes = "")
    public Employee findById(@PathVariable("id") Long id){
        Employee dept = es.loadById(id);
        return dept;
    }
    @GetMapping
    @ApiOperation(value = "查询全部",notes = "")
    public List<Employee> findAll(){
        List<Employee> list = es.loadAll();
        return list;
    }

    /**
     * 分页查询
     */
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "需要有currentPage和pageSize两个参数")
    public PageList<Employee> pagefind(@RequestBody EmployeeQuery para){
        PageList<Employee> pageList = es.queryData(para);
        return pageList;
    }
}
